package com.lq.ecoldchain.ui.main.introduce

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.view.ViewGroup
import com.lq.ecoldchain.R
import com.lq.ecoldchain.base.view.BaseFragment
import com.lq.ecoldchain.config.AppConfig
import com.lq.ecoldchain.databinding.FragmentIntroductionBinding
import com.lq.ecoldchain.entity.CarType
import com.lq.ecoldchain.entity.City
import com.lq.ecoldchain.entity.EventAll
import com.lq.ecoldchain.ui.goodstype.GoodsTypeListActivity
import com.lq.ecoldchain.ui.mine.carInfo.carline.city.CityAllActivity
import com.lq.ecoldchain.ui.mine.carInfo.typelist.CarTypeListActivity
import com.lq.ecoldchain.util.StateUtil
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_introduction.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.kodein.di.Kodein
import org.kodein.di.generic.instance

/*
*2018/12/24 0024  18:30
*一键介绍 by lq
*/
class IntroductionFragment : BaseFragment<FragmentIntroductionBinding>() {
    override val layoutId = R.layout.fragment_introduction
    override val viewDelegate: IntroductionDelegate by instance()
    private var isStart = true
    override val kodein = Kodein.lazy {
        extend(parentKodein)
        import(introductionKodeinModule)
    }

    override fun initView() {
        binding.delegate = viewDelegate
        initListener()
        EventBus.getDefault().register(this)
    }

    private fun initListener() {
        introduceStartRl.setOnClickListener {
            isStart = true
            startActivityForResult(Intent(it.context, CityAllActivity::class.java), AppConfig.INTRODUCE_PLACE_CODE)
        }
        introduceEndRl.setOnClickListener {
            isStart = false
            startActivityForResult(Intent(it.context, CityAllActivity::class.java), AppConfig.INTRODUCE_PLACE_CODE)
        }
        introduceCarTypeRl.setOnClickListener {
            startActivityForResult(Intent(it.context, CarTypeListActivity::class.java), AppConfig.INTRODUCE_CARTYPE_CODE)
        }
        goodsTypeRl.setOnClickListener {
            startActivity(Intent(it.context, GoodsTypeListActivity::class.java))
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == AppCompatActivity.RESULT_OK) {
            when (requestCode) {
                AppConfig.INTRODUCE_PLACE_CODE -> {
                    //获取
                    val city = data?.getParcelableExtra<City>("data")
                    if (isStart) {
                        viewDelegate.viewModel.startPlace.value = city?.cityName
                        viewDelegate.viewModel.startId.value = city?.id
                    } else {
                        viewDelegate.viewModel.endPlace.value = city?.cityName
                        viewDelegate.viewModel.endId.value = city?.id
                    }
                }
                AppConfig.INTRODUCE_CARTYPE_CODE -> {
                    val carType = data?.getParcelableExtra<CarType>("data")
                    if (carType != null) {
                        viewDelegate.viewModel.carTypeId.value = carType.id.toString()
                    }
                }
            }

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun goodsItemChangeEvent(event: EventAll.GoodsTypeItem) {
        viewDelegate.viewModel.type.value = event.type.id.toString()
        viewDelegate.viewModel.typeName.value = event.type.name
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}